﻿using System;

using UnityEngine;

public class MaxRewardUnit : MonoBehaviour
{

    private string rewardedAdUnitId;

    private string alias;

    public float retryTime = 3f;
    public Action<string, string, int> callback;

    // Use this for initialization
    public void Init(string adUnitId, string name)
    {
        
        rewardedAdUnitId = adUnitId;
        alias = name;

        if (string.IsNullOrEmpty(adUnitId))
        {
            Debug.LogError(alias + " 激励广告UnityId为空");
            return;
        }
        
#if UNITY_IOS || UNITY_ANDROID || UNITY_IPHONE 

        if (MaxSdk.IsInitialized())
        {
            InitializeRewardedAds();
        }
        else
        {
            MaxSdkCallbacks.OnSdkInitializedEvent += Initialize;
        }
#endif
    }


    private void OnDestroy()
    {
#if UNITY_IOS || UNITY_ANDROID || UNITY_IPHONE 
        MaxSdkCallbacks.OnSdkInitializedEvent -= Initialize;
        MaxSdkCallbacks.OnRewardedAdLoadedEvent -= OnRewardedAdLoadedEvent;
        MaxSdkCallbacks.OnRewardedAdLoadFailedEvent -= OnRewardedAdFailedEvent;
        MaxSdkCallbacks.OnRewardedAdFailedToDisplayEvent -= OnRewardedAdFailedToDisplayEvent;
        MaxSdkCallbacks.OnRewardedAdDisplayedEvent -= OnRewardedAdDisplayedEvent;
        MaxSdkCallbacks.OnRewardedAdClickedEvent -= OnRewardedAdClickedEvent;
        MaxSdkCallbacks.OnRewardedAdHiddenEvent -= OnRewardedAdDismissedEvent;
        MaxSdkCallbacks.OnRewardedAdReceivedRewardEvent -= OnRewardedAdReceivedRewardEvent;
#endif
    }

    public bool ShowAds()
    {
#if UNITY_IOS || UNITY_ANDROID || UNITY_IPHONE 
        if (MaxSdk.IsRewardedAdReady(rewardedAdUnitId))
        {
            MaxSdk.ShowRewardedAd(rewardedAdUnitId);
            return true;
        }
        else
        {
            return false;
        }
#else
        return false;
#endif
    }

    public bool IsReady()
    {
#if UNITY_IOS || UNITY_ANDROID || UNITY_IPHONE 
        return MaxSdk.IsRewardedAdReady(rewardedAdUnitId);
#else
        return true;
#endif
    }

    void Initialize(MaxSdkBase.SdkConfiguration sdkConfiguration)
    {
        InitializeRewardedAds();
    }


    public void InitializeRewardedAds()
    {
#if UNITY_IOS || UNITY_ANDROID || UNITY_IPHONE 
        // Attach callback
        MaxSdkCallbacks.OnRewardedAdLoadedEvent += OnRewardedAdLoadedEvent;
        MaxSdkCallbacks.OnRewardedAdLoadFailedEvent += OnRewardedAdFailedEvent;
        MaxSdkCallbacks.OnRewardedAdFailedToDisplayEvent += OnRewardedAdFailedToDisplayEvent;
        MaxSdkCallbacks.OnRewardedAdDisplayedEvent += OnRewardedAdDisplayedEvent;
        MaxSdkCallbacks.OnRewardedAdClickedEvent += OnRewardedAdClickedEvent;
        MaxSdkCallbacks.OnRewardedAdHiddenEvent += OnRewardedAdDismissedEvent;
        MaxSdkCallbacks.OnRewardedAdReceivedRewardEvent += OnRewardedAdReceivedRewardEvent;
#endif
        // Load the first RewardedAd
        LoadRewardedAd();
    }

    public void LoadRewardedAd()
    {
#if UNITY_IOS || UNITY_ANDROID || UNITY_IPHONE 
        MaxSdk.LoadRewardedAd(rewardedAdUnitId);
#endif
    }

    private void OnRewardedAdLoadedEvent(string adUnitId)
    {
        if (adUnitId != rewardedAdUnitId) return;
        // Rewarded ad is ready to be shown. MaxSdk.IsRewardedAdReady(rewardedAdUnitId) will now return 'true'
        if (callback != null)
        {
            callback("OnRewardedAdLoadedEvent", adUnitId, 0);
        }
    }

    private void OnRewardedAdFailedEvent(string adUnitId, int errorCode)
    {
        if (adUnitId != rewardedAdUnitId) return;
        // Rewarded ad failed to load. We recommend re-trying in 3 seconds.
        if (callback != null)
        {
            callback("OnRewardedAdFailedEvent", adUnitId, errorCode);
        }
        Invoke("LoadRewardedAd", retryTime);
    }

    private void OnRewardedAdFailedToDisplayEvent(string adUnitId, int errorCode)
    {
        if (adUnitId != rewardedAdUnitId) return;
        // Rewarded ad failed to display. We recommend loading the next ad
        if (callback != null)
        {
            callback("OnRewardedAdFailedToDisplayEvent", adUnitId, errorCode);
        }
        LoadRewardedAd();
    }

    private void OnRewardedAdDisplayedEvent(string adUnitId)
    {
        if (adUnitId != rewardedAdUnitId) return;
        if (callback != null)
        {
            callback("OnRewardedAdDisplayedEvent", adUnitId, 0);
        }
    }

    private void OnRewardedAdClickedEvent(string adUnitId)
    {
        if (adUnitId != rewardedAdUnitId) return;
        if (callback != null)
        {
            callback("OnRewardedAdClickedEvent", adUnitId, 0);
        }
    }

    private void OnRewardedAdDismissedEvent(string adUnitId)
    {
        if (adUnitId != rewardedAdUnitId) return;
        // Rewarded ad is hidden. Pre-load the next ad
        if (callback != null)
        {
            callback("OnRewardedAdDismissedEvent", adUnitId, 0);
        }
        LoadRewardedAd();
    }

    private void OnRewardedAdReceivedRewardEvent(string adUnitId, MaxSdk.Reward reward)
    {
        if (adUnitId != rewardedAdUnitId) return;
        // Rewarded ad was displayed and user should receive the reward
        if (callback != null)
        {
            callback("OnRewardedAdReceivedRewardEvent", adUnitId, 0);
        }
    }
}